@chcp 65001 >nul
@echo off
setlocal enabledelayedexpansion

set "base_path=%cd%"
set "types=java"

:parse_args
if "%~1"=="" goto after_parse

:: 匹配参数
if /i "%~1"=="-a" (
    if "%~2"=="" goto usage
    set "base_path=%~2"
    shift
    shift
    goto parse_args
)

if /i "%~1"=="-b" (
    if "%~2"=="" goto usage
    set "base_path=%cd%\%~2"
    shift
    shift
    goto parse_args
)

if /i "%~1"=="-s" (
    if "%~2"=="" goto usage
    set "base_path=!base_path!\%~2"
    shift
    shift
    goto parse_args
)

if /i "%~1"=="-t" (
    if "%~2"=="" goto usage
    set "types=%~2"
    shift
    shift
    goto parse_args
)

if /i "%~1"=="-h" (
    goto usage
)

:: 未知参数
echo [ERROR] Unknown argument: %~1
goto usage

:after_parse
setlocal enabledelayedexpansion
set "types=%types:,= %"
set /a total=0

for %%T in (%types%) do (
    set /a count=0
    for /r "%base_path%" %%F in (*.%%T) do (
        if exist "%%F" (
            for /f %%L in ('find /v /c "" ^< "%%F"') do (
                set /a count+=%%L
            )
        )
    )
    echo %%T code lines: !count!
    set /a total+=count
)
echo total code lines: %total%
endlocal

goto end

:usage
echo Usage:
echo   clc.bat [options]
echo     -a [absolute_dir]
echo     -b [base_dir]
echo     -s [sub_dir]
echo     -t [file_types, e.g. java,py,cpp]
echo     -h Show help
echo.
echo Examples:
echo   clc.bat
echo   clc.bat -t "java,cpp,py"
echo   clc.bat -a C:\MyProject -t "java,xml,properties"
goto end

:end
endlocal
